<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/27
 * Time: 17:16
 */
namespace app\appmsg\controller;

use app\common\helper\appmsg_helper;
use app\common\helper\emotion_helper;
use app\common\model\Appmsg;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Add extends AdminBase
{
    public function __call($action = '', $params = array())
    {
        $id        = Request::param('id', 0);
        $to_bottom = Request::param('to_bottom', 0);

        if ($id) {
            $appmsg_info = _uri('appmsg', ['id' => $id]);
            if (!$appmsg_info) {
                return _return('该图文信息不存在');
            }

            if ($this->store_id && $appmsg_info['store_id'] != $this->store_id) {
                return _return('你不能更改其它店铺的图文信息');
            }

            // 如果是关联图文
            if ($appmsg_info['relation_id']) {
                $relation_info = _uri('appmsg', ['id' => $appmsg_info['relation_id']]);
                if (!$relation_info) {
                    return _return('该图文关联的信息不存在，或已删除');
                }

                $appmsg_info = appmsg_helper::merge_appmsg($appmsg_info, $relation_info);
            }

            if (isset($appmsg_info['content']) && $appmsg_info['content']) {
                $appmsg_info['content'] = emotion_helper::code_to_emoji($appmsg_info['content']);
            }

            View::assign('appmsg_info', $appmsg_info);

            if ($appmsg_info['type'] == 2) {
                $son_list = appmsg_helper::get_merge_appmsg_list($id, 1);
                foreach ($son_list as $k=>$v) {
                    if (isset($v['content']) && $v['content']) {
                        $son_list[$k]['content'] = emotion_helper::code_to_emoji($v['content']);
                    }
                }

                //an_dump($son_list);
                View::assign('son_list', $son_list);
            }

            $type = (int)$appmsg_info['type'];
        } else {
            $type = (int)$action;
        }

        if (!in_array($type, array(1, 2))) {
            $type = 1;
        }

        View::assign([
            'type' => $type,
            'to_bottom' => $to_bottom
        ]);

        if ($type == 1) {

            return View::fetch('add_single');
        } else {

            return View::fetch('add_multi');
        }
    }
}